package servlet;

import bean.Category;
import bean.Property;
import bean.PropertyValue;
import util.Page;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.List;

public class PropertyServlet extends BaseBackServlet {
    @Override
    public String add(HttpServletRequest request, HttpServletResponse response, Page page)
    {
        int id = Integer.parseInt(request.getParameter("cid"));
        String name = request.getParameter("property_name");
//        try {
//            name =  new String(name.getBytes("ISO-8859-1"), "UTF-8");
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
        Property property = new Property();
        Category category = categoryDAO.get(id);
        property.setName(name);
        property.setCategory(category);
        propertyDAO.add(property);
//        System.out.println(property);
        return "@admin_property_list?cid="+id;
    }

    @Override
    public String delete(HttpServletRequest request, HttpServletResponse respone, Page page) {
        int id = Integer.parseInt(request.getParameter("ptid"));
        int cid = Integer.parseInt(request.getParameter("cid"));
//        System.out.println(id+" == "+cid);
        boolean result = false;
        String str = "";
        Property property = propertyDAO.get(id);
//        System.out.println(property);
//        List<PropertyValue> values = propertyValueDAO.list();
//        for (PropertyValue value:
//             values) {
//            if(value.getProperty().getId() == id)
//            {
//                result = true;
//                break;
//            }
//        }
//        if(result)
//            str = "@admin_property_list?delete=no&cid="+cid;
//        else
//        {
            propertyDAO.delete(id);
            str = "@admin_property_list?cid="+cid;
//        }
        return str;
    }

    @Override
    public String edit(HttpServletRequest request, HttpServletResponse response, Page page) {
        int cid = Integer.parseInt(request.getParameter("cid"));
        int id = Integer.parseInt(request.getParameter("ptid"));
        Property property = propertyDAO.get(id);
        Category category = property.getCategory();
        request.setAttribute("category",category);
        request.setAttribute("property",property);
        request.setAttribute("page", page);
        return "jsp/admin/editProperty.jsp";
    }

    @Override
    public String update(HttpServletRequest request, HttpServletResponse response, Page page) {
        int id = Integer.parseInt(request.getParameter("property_id"));
        String name = request.getParameter("property_name");
        try {
            name =  new String(name.getBytes("ISO-8859-1"), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Property property = propertyDAO.get(id);
        int cid = property.getCategory().getId();
        property.setName(name);
//        property.setCategory(category);
        propertyDAO.update(property);
//        System.out.println(property);
        return "@admin_property_list?cid="+cid;
    }

    @Override
    public String list(HttpServletRequest request, HttpServletResponse response, Page page) {
        int start = page.getStart();
        int count = page.getCount();
        int cid = Integer.parseInt(request.getParameter("cid"));
        int total = propertyDAO.getTotal(cid);
        page.setTotal(total);
        Category category = categoryDAO.get(cid);
        page.setParam("&cid="+category.getId());
//        System.out.println(start+"--"+count+"--"+total);
        List<Property> properties = propertyDAO.list(cid,start,count);
        request.setAttribute("properties", properties);
        request.setAttribute("category",category);
        request.setAttribute("page", page);
        return "jsp/admin/listProperty.jsp";
    }
}
